/*
 * @Description: 定时任务
 * @Author: lihk
 * @Date: 2024-09-26 13:42:17
 */
import { Injectable, Logger } from '@nestjs/common';
import { Cron } from '@nestjs/schedule';
import { BackupService } from '../backup/backup.service';
// import { EmailService } from '../email/email.service';

@Injectable()
export class TasksService {
  private readonly logger = new Logger(TasksService.name);
  constructor(
    private readonly backupService: BackupService,
    // private readonly emailService: EmailService
  ) {}

  // @Cron(CronExpression.EVERY_DAY_AT_MIDNIGHT)// 每天0点执行
  @Cron('0 03 15 * * *', {
    timeZone: 'Asia/Shanghai', // 指定时区，确保时间的准确性
  })
  handleCron() {
    this.logger.debug('Starting database backup');
    this.backupService.backupDatabase()
      .then(() => {
        this.logger.debug('Backup successful');
        // this.emailService.sendEmail('Backup successful', 'Database backup was successful.');
      })
      .catch(err => {
        this.logger.error('Backup failed', err);
        // this.emailService.sendEmail('Backup failed', `Error during backup: ${err.message}`);
      });
  }
}